name: copr
on:
  pull_request_target:
    types: [opened, synchronize, reopened]
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
  cancel-in-progress: true
env:
  COPR_ACCOUNT: '@authselect'
  COPR_PROJECT: pr${{ github.event.pull_request.number }}
  PR_ID: ${{ github.event.pull_request.number }}
  PR_URL: ${{ github.event.pull_request.html_url }}
jobs:
  prepare:
    runs-on: ubuntu-latest
    outputs:
      srpm: ${{ steps.srpm.outputs.file }}
      chroots_json: ${{ steps.chroots.outputs.json }}
    permissions:
      contents: read
    steps:
    - name: Checkout source
      uses: actions/checkout@v2
      with:
        ref: ${{ github.event.pull_request.head.ref }}
        repository: ${{ github.event.pull_request.head.repo.full_name }}

    - name: Build source rpm
      id: srpm
      uses: ./.github/actions/build-srpm
      with:
        version: 9.${{ env.COPR_PROJECT }}

    - name: Upload source rpm as an artifact
      uses: actions/upload-artifact@v2
      with:
        name: ${{ steps.srpm.outputs.file }}
        path: ${{ steps.srpm.outputs.path }}

    - name: Initialize copr actions
      id: copr
      uses: next-actions/copr/init@master
      with:
        token: ${{ secrets.COPR_SECRETS }}

    - name: Get copr chroots
      id: chroots
      uses: next-actions/copr/filter-chroots@master
      with:
        coprcfg: ${{ steps.copr.outputs.coprcfg }}
        filter: "fedora-.+-x86_64|centos-stream-.*-x86_64"
        exclude: "fedora-eln-.+"

    - name: Create copr project
      uses: next-actions/copr/create-project@master
      with:
        coprcfg: ${{ steps.copr.outputs.coprcfg }}
        chroots: ${{ steps.chroots.outputs.list }}
        project: ${{ env.COPR_PROJECT }}
        account: ${{ env.COPR_ACCOUNT }}
        fedora-review: off
        description: 'Development package for [authselect pull request #${{ env.PR_ID }}](${{ env.PR_URL }}).'
        instructions: 'Use this for test purpose only. Do not use this in production.'

    - name: Cancel pending builds
      uses: next-actions/copr/cancel-builds@master
      with:
        coprcfg: ${{ steps.copr.outputs.coprcfg }}
        project: ${{ env.COPR_PROJECT }}
        account: ${{ env.COPR_ACCOUNT }}

  build:
    runs-on: ubuntu-latest
    needs: [prepare]
    strategy:
      matrix:
        chroot: ${{ fromJson(needs.prepare.outputs.chroots_json) }}
      fail-fast: false
    steps:
    - name: Checkout source
      uses: actions/checkout@v2

    - name: Downlooad source rpm
      uses: actions/download-artifact@v2
      with:
        name: ${{ needs.prepare.outputs.srpm }}
        path: .

    - name: Initialize copr actions
      id: copr
      uses: next-actions/copr/init@master
      with:
        token: ${{ secrets.COPR_SECRETS }}

    - name: Build srpm in copr for ${{ matrix.chroot }}
      uses: next-actions/copr/submit-build@master
      with:
        coprcfg: ${{ steps.copr.outputs.coprcfg }}
        srpm: ${{ needs.prepare.outputs.srpm }}
        chroots: ${{ matrix.chroot }}
        project: ${{ env.COPR_PROJECT }}
        account: ${{ env.COPR_ACCOUNT }}

  check:
    name: All copr builds are successful
    if: ${{ always() }}
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - name: Fail on failure
        if: ${{ needs.build.result != 'success' }}
        run: exit 1
